/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "interface/network_adapter.h"

namespace dev {

bool NetworkAdapter::update_firmware_version_by_ncsi() {
    ncsi_version_id_info_t version_id = {};
    bool ret = m_ncsi_over_mctp_huawei->get_version_id(DEFAULT_PACKAGE_ID, DEFAULT_CHANNEL_ID,
                                                       &version_id);
    if (!ret) {
        elog("Error: Failed to update version id");
        return false;
    }
    FirmwareVersion.set_value(version_id.firmware_version);
    return true;
}

void NetworkAdapter::start_ncsi_update_task_huawei(ncsi_over_mctp_hw_ptr ncsi_over_mctp,
                                                   mc::milliseconds      interval) {
    m_ncsi_timer            = new mc::core::timer(this);
    m_ncsi_over_mctp_huawei = ncsi_over_mctp;
    m_ncsi_timer->timeout.connect([this]() {
        update_firmware_version_by_ncsi();
    });
    m_ncsi_timer->set_single_shot(false);
    m_ncsi_timer->start(interval);
}

void NetworkAdapter::stop_ncsi_update_task() {
    if (m_ncsi_timer) {
        m_ncsi_timer->stop();
        delete m_ncsi_timer;
        m_ncsi_timer = nullptr;
    }
}

void NetworkAdapter::update_firmware_version_by_smbus() {
    // 使用静态变量缓存，减少频繁创建
    static mc::mutable_dict request_template{{"opcode", 0x05}, {"expect_data_len", 3}};
    auto rsp = m_smbus_obj->send_request(request_template);
    if (rsp.empty()) {
        elog("Error: Failed to update firmware version by smbus");
        return;
    }
    // 解析rsp， 例如返回值时 \x11\x45\x02 其最终计算结果为 17.6.9.2
    struct rsp_firmeware {
        uint8_t major_ver;
        uint8_t minor_ver;
        uint8_t update_ver;
    } firmware_rsp;

    auto firmware_str = reinterpret_cast<rsp_firmeware*>(rsp.data());
    char buffer[16]   = {0};
    snprintf(buffer, sizeof(buffer), "%d.%d.%d.%d", firmware_str->major_ver,
             firmware_str->minor_ver / 10, firmware_str->minor_ver % 10, firmware_str->update_ver);
    std::string firmware_version(buffer);
    elog("update firmware version: ${firmware_version}", ("firmware_version", firmware_version));
    FirmwareVersion.set_value(firmware_version);
}

void NetworkAdapter::start_smbus_update_task(smbus_obj_ptr smbus_obj, mc::milliseconds interval) {
    if (!smbus_obj) {
        elog("Error: smbus_obj is not initialized");
        return;
    }
    // 保存传入的smbus_obj到成员变量
    m_smbus_obj = smbus_obj;

    m_smbus_timer = new mc::core::timer(this);
    m_smbus_timer->timeout.connect([this]() {
        update_firmware_version_by_smbus();
    });
    m_smbus_timer->set_single_shot(false);
    m_smbus_timer->start(interval);
}

void NetworkAdapter::stop_smbus_update_task() {
    if (m_smbus_timer) {
        m_smbus_timer->stop();
        delete m_smbus_timer;
        m_smbus_timer = nullptr;
    }
}

} // namespace dev

MC_REFLECT(dev::NetworkAdapter)